package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.dao.OrgLeaderMapper;
import com.yjt.admin.system.entity.OrgLeader;
import com.yjt.admin.system.service.OrgLeaderService;
import com.yjt.admin.system.vo.org.OrgLeaderVO;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.utils.ComUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 组织领导关系表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-14
 */
@Service
public class OrgLeaderServiceImpl extends ServiceImpl<OrgLeaderMapper, OrgLeader> implements OrgLeaderService {

    @Resource
    private OrgLeaderMapper orgLeaderMapper;

    @Override
    public void addOrgLeader(List<String> orgLeaderIdList, String orgId) {

        //保存集团领导/公司领导/部门领导--可选择人员，岗位并且可多选
        if (ComUtil.isNotNullList(orgLeaderIdList)) {
            List<OrgLeader> orgLeaderPoList = orgLeaderIdList.stream().map(orgLeaderId -> {
                return new OrgLeader(null, orgLeaderId, orgId);
            }).collect(Collectors.toList());
            this.saveBatch(orgLeaderPoList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrgLeader(List<String> orgLeaderIdList, String orgId) {

        //保存集团领导/公司领导/部门领导--可选择人员，岗位并且可多选
        if (ComUtil.isNotNullList(orgLeaderIdList)) {

            //先移除关系表中的数据
            this.remove(Wrappers.<OrgLeader>lambdaUpdate().eq(OrgLeader::getOrgId, orgId));

            //保存集团领导关系表中的数据
            List<OrgLeader> orgLeaderPoList = orgLeaderIdList.stream().map(orgLeaderId -> {
                return new OrgLeader(null, orgLeaderId, orgId);
            }).collect(Collectors.toList());
            this.saveBatch(orgLeaderPoList);
        }
    }

    @Override
    public void setOrgLeader(OrgVO orgVO) {
        //查询集团领导/公司领导/部门领导
        if (ComUtil.isNotNullStr(orgVO.getOrgLeaderIds())) {
            List<String> orgLeaderIdList = Arrays.asList(orgVO.getOrgLeaderIds().split(","));
            List<OrgLeaderVO> orgLeaderVOList = orgLeaderMapper.findOrgLeaderById(orgLeaderIdList);
            List<String> orgLeaderNameList = new ArrayList<>();
            orgLeaderIdList.stream().forEach(orgLeaderId -> {
                OrgLeaderVO orgLeaderVO = orgLeaderVOList.stream().filter(
                        OrgLeaderVO -> StringUtils.equals(orgLeaderId, OrgLeaderVO.getEmplyOrPostId())).findFirst().get();
                orgLeaderNameList.add(orgLeaderVO.getEmpOrPostName());
            });
            orgVO.setOrgLeaderIdList(orgLeaderIdList);
            orgVO.setOrgLeaderNameList(orgLeaderNameList);
        }
    }

}
